layui.use(['layer'], function() {
	var $ = layui.$,
		layer = layui.layer;
	$(function() {
		//回到顶部
		utilshowhide()
        var p=0;t=0;
		$(window).scroll(function() {
			utilshowhide()
			p=$(this).scrollTop();
            if(t<=p){
                $('.myheader').addClass('slideUp')
            }
            else{
            	$('.myheader').removeClass('slideUp')
            }
            t = p;
		})
		$('.layui-fixbar .layui-fixbar-top').click(function() {
			$("html,body").animate({
				scrollTop: 0
			}, 300);
		})

		function utilshowhide() {
			console.log($(document).scrollTop())
			if($(document).scrollTop() > 260) {
				$('.layui-fixbar').show()
				$('.myheader').removeClass('is_top_true')
				$('.myheader').addClass('is_top_false')
			} else {
				$('.layui-fixbar').hide()
				$('.myheader').removeClass('is_top_false')
				$('.myheader').addClass('is_top_true')
			}
		}
		//tips
		$('#grid .content span a').hover(function() {
			layer.tips($(this).attr('atitle'), this, {
				tips: [1, '#F75733'],
				time: 100000
			});
		}, function() {
			layer.closeAll('tips')
		})
		//navs
		$('#navs').click(function() {
			var that = this;
			if($(that).hasClass('show')) {
				$(that).parent('.layui-nav').parent('.right').siblings('.left').slideUp(300,function(){
					$(that).removeClass('show')
				})
			} else {
				$(that).parent('.layui-nav').parent('.right').siblings('.left').slideDown(300, function() {
					$(that).addClass('show')
				})
			}
		})
		//防止小屏关闭nav后大屏无法显示
		$(window).resize(function() {
			if($(window).width()>=768){
				$('.left').css({display: 'block'})
			}else{
				$('.left').css({display: 'none'})
			}
		});
		/////////////////////////////////////////////
		$.ajaxSetup({
			cache: false,
			timeout: 8000,
			type: "post",
			contentType: "application/json; charset=utf-8",
			dataType: 'json',
			headers: {
				"token": localStorage.getItem("token")
			},
			beforeSend: function(xhr) {
				layer.load();
			},
			error: function(xhr, textStatus, errorThrown) {
				layer.closeAll('loading');
				var msg="未知错误";
				if(xhr.responseText==null||xhr.responseText==""){
					msg="链接超时";
				}else{
					var response = JSON.parse(xhr.responseText);
					msg=response.msg;
					if (response.code == 401) {
						localStorage.removeItem("token");
					}
				}
				layer.msg(msg, {
					icon: 2,
					time: 2000
				});
			}
		});
	})
});